/**
 * 管理员 管理用户界面js
 */
var user; //用户名
var pwd1; //密码
var url = '';
var jsonData = '';
$(function(){
	
	 
	 user = document.getElementById("addUserName"); 
	 user.onblur = CheckAddUserName;
	 
	 pwd1 = document.getElementById("addUserPwd");
	 pwd1.onblur = CheckAddUserPwd;
 
	 // 设置加载页面 就查询出所有的用户
	 selectAllUser();
	 
});

 //密码
	function CheckAddUserPwd() {
		var pwd = pwd1.value;
		var span = document.getElementById("addUserPwdSpan");
		span.innerHTML = "";
		if (trim(pwd) == "") {
			span.className = "error";
			span.innerHTML = "密码不能为空";
		} else if (trim(pwd).length != pwd.length) {
			span.className = "error";
			span.innerHTML = "密码中不能含有空格";
		} else if (trim(pwd).length < 6 || trim(pwd).length > 20) {
			span.className = "error";
			span.innerHTML = "密码长度为6-20个字符";
		} else {
			span.className = "success";
			span.innerHTML = " ";
		}
	}
//用户名
	function CheckAddUserName() {
		var formaccount = user.value;

		var span = document.getElementById("addUserNameSpan");
		span.innerHTML = "";

		if (trim(formaccount) == "") {
			span.className = "error";
			span.innerHTML = "用户名不能为空";
		} else if (trim(formaccount).length < 2) {
			span.className = "error";
			span.innerHTML = "用户名不能小于2个字符";
		} else {
			$.ajax({
				type : "POST",
				url : "xxxxxx",
				data : {
					"action" : "isSameName",
					"username" : formaccount
				},
				dateType : "json",
				success : function(data) {

					if (data == 1) {
						span.className = "success";
						span.innerHTML = " ";
					} else {
						span.className = "error";
						span.innerHTML = "用户名已被注册！";
					}

				}
			});

		}

	}

	//正则表达式
	//判断 输入不能为空
	function trim(txt) {
		var afterTrimTxt = txt.replace(/^\s*/, "").replace(/\s*$/, "");
		return afterTrimTxt;
	}
	
	function regist() {
		//用户点击注册按钮时 要把所有的信息都验证一遍
		CheckAddUserName();
		CheckAddUserPwd();
	 
		//通过 span 的class 属性  为 error的个数 来判断 是否可以提交
		var errorSpanArr = document.getElementsByClassName("error");
		var registForm = document.getElementById("addUser-form");
		/* alert(errorSpanArr.length); */
		if (errorSpanArr.length <= 0) {
			//提交
			$("#addUser-form").attr("action","/userManage/addUser");
			//alert("提交");
			registForm.submit();
			/* $("#register-form").submit(); */
		} else {
			//不提交
			//alert("不提交"); 
			return " ";
		}
	}
	
	
	function commonAjax(){
		var result = '';
		$.ajax({
			type: 'GET',
			//data: jsonData,
		    url: url,
		    async: false,
		    dataType:"json",
		    success: function(data){
		    	result = data;
		    },
		    error: function(){
		    }
		});
		return result;
	}

	function selectAllUser(){
		url = ctx+'/userManage/findAllUsers';
		var dataObj = commonAjax();
		initAllUser(dataObj.userList);
	 }	
	
	
	function selectUserAjax(managerUser_name,selectGroup){
		var result = '';
		$.ajax({
			type: 'GET',
			data: {
					managerUser_name:managerUser_name,
					selectGroup:selectGroup
					},
		    url: url,
		    async: false,
		    dataType:"json",
		    success: function(data){
		    	result = data;
		    },
		    error: function(){
		    }
		});
		return result;
	}
function selectUser(){
	//alert("in selectUser");
	var managerUser_name = $("#managerUser_name").val();
	var selectGroup = $("#selectGroup").val();
	url = ctx+'/userManage/findUsers';
	//jsonData = "{managerUser_name:"+managerUser_name+",selectGroup:"+selectGroup+"}";
	var dataObj = selectUserAjax(managerUser_name,selectGroup);
	
	initAllUser(dataObj.userList);
 }

function initAllUser(data) {
	
	 var returnHtml = '';
	 
	$.each(data,function(idx,item){
	   //输出 
		returnHtml += "<tr><td>"+item.userName+"</td>" +
		"<td>"+item.passwd+"</td>" +
		"<td>"+item.department+"</td>" +
		"<td>"+item.authority+"</td>" +
		"<td>"+"<span id=' '>"+item.state+"</span>"+" / <button id='btn"+idx+"' class='glyphicon glyphicon-retweet' value='"+item.id+"' title='切换状态' onclick='turnState(this);'></button>"+"</td>" +
		"<td>"+"<button onclick='getOneForUpdate("+item.id+")'  class='btn btn-success btn-xs'>修改</button>"+"</td>"+
		"<td>"+"<button onclick='deleteReaderById("+item.id+")' class='btn btn-danger btn-xs'>删除</button>"+"</td></tr>";
		//data-toggle='modal' data-target='#alterUser'
	    $('#info').html(returnHtml);
	});
	
}

function getOneForUpdateAjax(id){
	var result = '';
	$.ajax({
		type: 'GET',
		data: {id:id},
	    url: url,
	    async: false,
	    dataType:"json",
	    success: function(data){
	    	result = data;
	    },
	    error: function(){
	    }
	});
	return result;
}
function getOneForUpdate(id){
	/**
	 * 通过id 查询 该用户信息
	 */
	url = '/userManage/findUserById';
	
	
	var returnData = getOneForUpdateAjax(id);
	initAlterUserModel(returnData);
	
    $('#alterUser').modal('show');
}

function updateUser() {
	$("#alterUser-form").attr("action","/userManage/updateUserById");
	//alert("提交");
	//registForm.submit();
}

function deleteReaderById(id) {
			  
			$.confirm({
	        	title: ' ',
	        	content: '	您确认删除吗？',
					confirm: function(){
					//location.assign("delete.action?id=" + id );
						 window.location.href = "/userManage/deleteUser?id="+id;
		 			},
		 			cancel: function(){
				
				}
		});
	}


function initAlterUserModel(data){
	var innerHtml = '';
	 // var alterDiv = document.getElementById("alter_user_div");  
	  innerHtml += 
	'<div class="modal fade" id="alterUser" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'+
	'<div class="modal-dialog" style="margin-top:200px;">'+
		'<div class="modal-content">'+
			'<div class="modal-header">'+
				'<button type="button" class="close" data-dismiss="modal" '+
					'aria-hidden="true">×'+
				'</button>'+
				'<h4 class="modal-title" id="myModalLabel">'+
				'修改用户'+
				'</h4>'+
			'</div>'+
			'<form action="" id="alterUser-form">'+
				'<div class="modal-body input-group">'+
					' <label class="form-control glyphicon glyphicon-user" style="width: 50px;margin-left: 110px;margin-top:-1px;"> </label>'+
		            '  <input id="addUserName" type="text" name="user_name" class="form-control" style="width: 300px;border-top-right-radius:4px;border-bottom-right-radius:4px;" value="'+data.user.userName+'" placeholder="输入用户名"/>'+
										'	  <br/>	<span id="addUserNameSpan" style="height:38px;">&nbsp</span><br/>'+
					'<label class="form-control glyphicon glyphicon-lock" style="width: 50px;margin-left:110px;margin-top:-1px;"> </label>'+
		           '  <input id="addUserPwd" type="text" name="passwd" class="form-control" style="width: 300px;" value="'+data.user.passwd+'" placeholder="输入密码"/>'+
		            '  <br/>	<span id="addUserPwdSpan" style="height:38px;">&nbsp</span><br/>'+
		            ' <label class="form-control glyphicon glyphicon-folder-close" style="width: 50px;margin-left:110px;margin-top:-1px;"> </label>'+
		            	'<select id="select_tiaojian" class="form-control" name="department" style="width: 300px;">'+
                                 '<option id="section1">研发一部</option>'+
                                 '<option id="section2">研发二部</option>'+
                                 '<option id="section3">研发三部</option>'+
                                 '<option id="section4">人力资源一部</option>'+
                             
                       '</select> '+
		           ' <br/><br/><br/>'+
		            '<label class="form-control glyphicon glyphicon-wrench" style="width: 50px;margin-left:110px;margin-top:-1px;"> </label>'+
			        '<label class="checkboxLabel"><input name="" type="checkbox" value="登录权限" />登录权限 </label> '+
					'<label class="checkboxLabel"><input name="" type="checkbox" value="搜索权限" />搜索权限</label> <br/>'+
					'<label class="checkboxLabel" style="margin-left:170px;"><input name="" type="checkbox" value="修改简历权限" />修改简历权限</label> '+
					'<label class="checkboxLabel"><input name="" type="checkbox" value="标记简历权限" />标记简历权限</label> '+
					'<input type="hidden" name="id" value="'+data.user.id+'" />'+
					'<input type="hidden" name="state" value="'+data.user.state+'" />'+
					'<input type="hidden" name="authority" id="updateCheck" value="" />'+
					'</div>'+
				'<div class="modal-footer">'+
				'	<button type="button" class="btn btn-default" '+
					'data-dismiss="modal">关闭'+
					'</button>'+
					
					'<button class="btn btn-primary" onclick="updateUser()">'+
					'修改'+
					'</button>'+
				'</div>'+
				'<script>'+
				  '$("input[type=checkbox]").change(function(){'+
				   '$("#updateCheck").val($("input[type=checkbox]:checked").map(function(){return this.value;}).get().join(","));'+
				  '})'+
				'</script>'+
			'</form>'+
		'</div>'+
	'</div>'+
'</div>	';
	  
	  $("#alter_user_div").html(innerHtml);
	  
	  if (data.user.department=="研发一部") {//data.section
		$("#section1").attr("selected","selected");
	    }
	  if (data.user.department=="研发二部") {//data.section
			$("#section2").attr("selected","selected");
		}
	  if (data.user.department=="研发三部") {//data.section
			$("#section3").attr("selected","selected");
		}
	  
	  if (data.user.department=="人力资源一部") {//data.section
			$("#section4").attr("selected","selected");
		}
}

/**
 * 切换 用户状态
 */
function turnState(data) {
	url = ctx+'/userManage/toggleState';
		var btn = document.getElementById(data.id);
		var span = btn.previousSibling.previousSibling;
		var id = data.value;
		var showState = span.innerHTML;

		if (showState=="正常") {
			// 传给后台的操作 
			//然后查询所有
			turnStateAjax(id,showState);
			span.innerHTML = "冻结";
		}
		if (showState=="冻结") {
		// 传给后台的操作 
		//然后查询所有
			turnStateAjax(id,showState);
			span.innerHTML = "正常";
		}
	
}


function turnStateAjax(id,state){
	
	$.ajax({
		type: 'GET',
		data: {
			id:id,
			state:state
		},
	    url: url,
	    async: false,
	    dataType:"json",
	    success: function(data){
	    	
	    },
	    error: function(){
	    }
	});
	
}
